<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { propApi } from '@/api/controller';
  import { ToastSuccess, Toastloading, allNumber } from '@/utils/public';
  import serachUserid from '@/components/serachUserid/index.vue';
  const formLabelWidth = '160px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
  })
  interface formQuert {
    user_id:number
    diamond_num:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    user_id: [{ required: true, message: "请输入用户ID", trigger: "blur" }],
    diamond_num: [{ required: true, message: "请输入钻石数", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      await propApi.luckGiftAddWhite(form).then(()=>{
        loading.close()
        ToastSuccess('新增用户白名单成功')
        $emit('updateList')
        closeDialog()
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const openDialog = ()=> {
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="40%"
      title="新增用户白名单">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" prop="user_id" label="用户ID">
          <serachUserid v-model:user_id="form.user_id" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" prop="diamond_num" label="钻石数">
          <el-input @input="allNumber(form,'diamond_num')" v-model="form.diamond_num" placeholder="请输入钻石数" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>